﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using prototype123.models;
using System.Data.SqlClient;

namespace prototype123.data_access
{
    //This class handles all data access between the application and the
    //textbook table in the database.
    class TextbookDAO
    {
        private SqlConnection db_connection; // = new SqlConnection("Data Source=sparky;Initial Catalog=cps420_team2_dev;Persist Security Info=True;User ID=cps420team2;Password=team2");

        public TextbookDAO(SqlConnection connection)
        {
            db_connection = connection;

        }

        /// <precondition>System needs a list of textbooks available to populate the GUI</precondition>
        /// <summary>
        /// Loads all textbooks from the database and returns them to the controller
        /// </summary>
        /// <returns>An ArrayList of strings - each string is one textbook name</returns>
        /// <postcondition>System now has a list of all available textbooks</postcondition>
        public ArrayList LoadAllTextbooks()
        {
            ArrayList textbooks = new ArrayList();

            SqlCommand getcourse = new SqlCommand("SELECT * FROM TEXTBOOK", db_connection);
            SqlDataReader reader = getcourse.ExecuteReader();
            while (reader.Read())
            {
                Textbook textbook = new Textbook();
                textbook.Title = (string)reader["TITLE"];
                textbook.ISBN = Convert.ToInt64(reader["ISBN"]);
                textbook.TextBookID = Convert.ToInt32(reader["TEXTBOOK_ID"]);
                textbooks.Add(textbook);
            }
            reader.Close();

            return textbooks;
        }
    }
}
